// type HttpResponse<T> = {
//   statusCode: number;
//   message: string;
//   data: T
// }

const useRequest = <T>(
  api: string,
  method: "GET" | "HEAD" | "PATCH" | "POST" | "PUT" | "DELETE" = "GET",
  params?: Record<string, number | string>,
  body?: Record<string, number | string>,
) => {
  return new Promise<T>((resolve, reject) => {
    const runtimeConfig = useRuntimeConfig();
    $fetch(api, {
      baseURL: runtimeConfig.public.baseURL, 
      method,
      params,
      body,
      onRequest(req) {
        console.log('req', req);
      },
      onResponse(result) {
        const res = result.response._data;
        if (res.statusCode === 200 || res.statusCode === 201) {
          return resolve(res.data);
        }
        reject(res);
      }
    });
  });
};

export const useRequestGet = <T>(api: string, params: Record<string, number | string>) => useRequest<T>(api, 'GET', params, undefined);
export const useRequestPost = <T>(api: string, body: Record<string, number | string>) => useRequest<T>(api, 'POST', undefined, body);
